<template>
  <div>
    <el-form ref="deptForm" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="form.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="form.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="form.manager"
          style="width: 80%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in employees"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="form.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
        <el-button size="small" @click="hCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDepartments } from '@/api/departments'
import { getDepartDetail, updateDepartments } from '@/api/departments'
export default {
  props: {
    id: { // 正在编辑的id
      type: String,
      required: true
    },
    isedit: { // 编辑状态
      type: Boolean,
      required: true
    },
    oragin: { // 传过来的不能重复的数组
      type: Array,
      required: true
    }

  },
  data() {
    // code表单重复验证
    const validCode = (rule, value, callback) => {
      // value就是当前用户输入的编码，它不能与现有的编码数据（originList）重复
      // 如果重复，就验证不通过
      let existCodeList = this.oragin.map(item => {
        return item.code
      })
      // 打印出来的是数组中所有的code
      console.log('existCodeList', existCodeList)
      if (this.isedit) {
        console.log('正在编辑的id', this.id)
        existCodeList = this.oragin.filter(item => item.id !== this.id).map(item => item.code)// 用过滤器，让当前正在编辑的id不等于所有对象里的id,不能重复，检查数组是否有重复的code
      }
      // if (value !== existCodeList.includes(value)) {
      //   callback()
      // } else {
      //   callback(new Error('编码' + value + '已经存在'))
      // }
      if (existCodeList.includes(value)) {
        callback(new Error('编码' + value + '已经存在'))
      } else {
        callback()
      }
    //   existCodeList.includes(value)
    //     ? callback(new Error('编码' + value + '已经存在'))
    //     : callback()
    }
    // name重复验证
    const validName = (rule, value, callback) => {
      const existNameList = this.oragin.filter(item => item.pid === this.id).map(item => item.name)// 用过滤器，让当前正在编辑的id不等于所有对象里的id,不能重复，检查数组是否有重复的名字
      console.log('validName.... , 当前被校验的名字是', value)
      console.log('被占用的名字列表', existNameList)
      if (existNameList.includes(value)) {
        callback(new Error('名称' + value + '已经存在'))
      } else {
        callback()
      }
    }

    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍

      },
      close: true,
      employees: [], // 列表数据

      rules: { // 表单验证
        name: [
          { required: true, message: '必填', tigger: 'blur' },
          { min: 1, max: 50, message: '请输入1-50个字符', tigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '必填', tigger: 'blur' },
          { min: 1, max: 50, message: '请输入1-50个字符', tigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '必填', tigger: 'blur' }

        ],
        introduce: [
          { required: true, message: '必填', tigger: 'blur' },
          { min: 1, max: 300, message: '请输入1-300个字符', tigger: 'blur' }
        ]

      }

    }
  },
  created() {
    this.loadEmployee()// 获去员工列表数据
    this.loadDetail() // 获取部门详情
  },
  methods: {
    //   员工列表数据
    async loadEmployee() {
      try {
        const res = await getEmployeeSimple()
        console.log('员工列表信息', res)
        this.employees = res.data
        console.log('你好', this.employees)
      } catch (err) {
        console.log(err)
      }
    },
    // 编辑数据
    async doAdd() {
      const res = await addDepartments({
        ...this.form, pid: this.id
      })
      console.log(res)
      this.$emit('genxin')// 传更新数据
    },
    // 获取部门详情
    async loadDetail() {
      if (this.isedit) {
        const res = await getDepartDetail(this.id)
        console.log('获取部门详情', res)

        this.form = res.data
      }
    },
    // 修改部门
    async doEdit() {
      try {
        const re = await updateDepartments(this.form)// 应为编辑数据的时候已经加入了id，所以直接使用就行
        console.log('修改部门', this.form, re)
        this.$emit('genxin')// 传更新数据
      } catch (err) {
        console.log(err)
      }
    },
    hSubmit() {
      this.$refs.deptForm.validate(vaild => {
        if (vaild) {
          // 如果通过了验证，然后在发送请求
          if (this.isedit) {
            this.doEdit()// 发送修改部门请求
          } else {
            this.doAdd()// 点击发送编辑请求
          }
        }
      })
    },
    hCancel() {
      this.$emit('close')
    }

  }

}
</script>

<style>
</style>
